<%@ Page language="vb" CodeFile="copyProduct.aspx.vb" Inherits="copyProduct_aspx_vb" %>

<%
    rowNumber = 0
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
        productID = Request.QueryString("productID")
%>
<script type="text/javascript">
  function confirmSubmit() { 
	if (window.confirm('Are you sure you want to make a copy of this product?')) {
		return true;
	}
	else {
		return false;
	}
  }
  </script>
<%
        If Len(productID) > 0 Then 
            productID = CLng(productID)
            dbOpen()
            RecSet = Conn.Execute("Select product From tblProduct Where productID = " & productID & "")
            If RecSet.EOF Then 
                productID = ""
            Else
                product = RecSet.Fields("product").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
        End IF
        Over()
        strJS = ""
        strJS = strJS + "[""product"", ""==''""],"
        checkFields()
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='1000'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>COPY PRODUCT<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='copyProduct.aspx?do=save&productID=" & productID & "' method='post' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr><td class='formBold'>Old product:</td><td align='right'><input type='text' name='oldProduct' id='oldProduct' size='40' maxlength='50' value='" & product & "' readonly='readonly'></td><td></td></tr>")
        Response.Write("<tr><td class='formBold'>New product:</td><td align='right'><input type='text' name='product' id='product' size='40' maxlength='50' value=''></td><td><div id='error_product' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td align='right' colspan='2'><input type='submit' value='Copy product' onClick='return confirmSubmit()'></td></tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "save" Then 
        product = Request.Form("product")
        productID = CLng(Request.QueryString("productID"))
        If Len(product) > 0 Then 
            product = Trim(product)
            product = Replace(CStr(product), "'", "")
            product = Replace(CStr(product), """", "")
            product = Replace(CStr(product), "<", "")
            product = Replace(CStr(product), ">", "")
            product = Replace(CStr(product), "=", "")
        End IF
        dbOpen()
    RecSet = Conn.Execute("Select countryID, riskCarrier, allowInvoiceDistancePenalty, allowInvoiceDurationPenalty,partnerSubvention,allowInvoiceServiceAccountBalance, allowInvoiceAdminFee From tblproduct Where productID = " & productID & "")

        If Not RecSet.EOF Then 
            countryID = RecSet.Fields("countryID").Value
            riskCarrier = RecSet.Fields("riskCarrier").Value
            partnerSubvention = RecSet.Fields("partnerSubvention").Value
            allowInvoiceDistancePenalty = RecSet.Fields("allowInvoiceDistancePenalty").Value
            allowInvoiceDurationPenalty = RecSet.Fields("allowInvoiceDurationPenalty").Value
            allowInvoiceServiceAccountBalance = RecSet.Fields("allowInvoiceServiceAccountBalance").Value
            allowInvoiceAdminFee = RecSet.Fields("allowInvoiceAdminFee").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
    Conn.Execute("Insert Into tblProduct (countryID, product, riskCarrier, allowInvoiceDistancePenalty, allowInvoiceDurationPenalty, allowInvoiceServiceAccountBalance, allowInvoiceAdminFee,partnerSubvention) Values(" & countryID & ",'" & product & "'," & riskCarrier & "," & allowInvoiceDistancePenalty & "," & allowInvoiceDurationPenalty & "," & allowInvoiceServiceAccountBalance & "," & allowInvoiceAdminFee & "," & partnerSubvention & ")")
        dbClose()
        dbOpen()
        RecSet = Conn.Execute("SELECT MAX(productID) AS newProductID FROM tblProduct")
        newProductID = RecSet.Fields("newProductID").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        RecSet = Conn.Execute("Select price, pricePerKmOverdue, adminFee, priceListCategory, priceListDuration, priceListDistance, uw, uwPerKmOverdue, cylinderVolumeFrom, cylinderVolumeTo From tblPriceList Where productID = " & productID & "")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                price = RecSet.Fields("price").Value
                pricePerKmOverdue = RecSet.Fields("pricePerKmOverdue").Value
                adminFee = RecSet.Fields("adminFee").Value
                priceListCategory = RecSet.Fields("priceListCategory").Value
                priceListDuration = RecSet.Fields("priceListDuration").Value
                priceListDistance = RecSet.Fields("priceListDistance").Value
                uw = RecSet.Fields("uw").Value
                uwPerKmOverdue = RecSet.Fields("uwPerKmOverdue").Value
                cylinderVolumeFrom = RecSet.Fields("cylinderVolumeFrom").Value
                cylinderVolumeTo = RecSet.Fields("cylinderVolumeTo").Value
                If Len(price) > 0 Then 
                    price = Trim(price)
                    price = Replace(CStr(price), ",", ".")
                Else
                    price = 0
                End IF
                If Len(pricePerKmOverdue) > 0 Then 
                    pricePerKmOverdue = Trim(pricePerKmOverdue)
                    pricePerKmOverdue = Replace(CStr(pricePerKmOverdue), ",", ".")
                Else
                    pricePerKmOverdue = 0
                End IF
                If Len(adminFee) > 0 Then 
                    adminFee = Trim(adminFee)
                    adminFee = Replace(CStr(adminFee), ",", ".")
                Else
                    adminFee = 0
                End IF
                If Len(uw) > 0 Then 
                    uw = Trim(uw)
                    uw = Replace(CStr(uw), ",", ".")
                Else
                    uw = 0
                End IF
                If Len(uwPerKmOverdue) > 0 Then 
                    uwPerKmOverdue = Trim(uwPerKmOverdue)
                    uwPerKmOverdue = Replace(CStr(uwPerKmOverdue), ",", ".")
                Else
                    uwPerKmOverdue = 0
                End IF
                Conn.Execute("Insert Into tblPriceList (productID, price, pricePerKmOverdue, adminFee, priceListCategory, priceListDuration, priceListDistance, uw, uwPerKmOverdue, cylinderVolumeFrom, cylinderVolumeTo) Values(" & newProductID & "," & price & "," & pricePerKmOverdue & "," & adminFee & ",'" & priceListCategory & "'," & priceListDuration & "," & priceListDistance & "," & uw & "," & uwPerKmOverdue & "," & cylinderVolumeFrom & "," & cylinderVolumeTo & ")")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Redirect("copyProduct.aspx?productID=" & newProductID & "&subDo=%20-%20SAVED")
    End IF
%>
